home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / Sources / src / lib / homeexp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  2.2 KB  |  121 lines

  1. /*{{{}}}*/
  2. /*{{{  #includes*/
  3. #ifdef CONFIG_H
  4. #   include "config.h"
  5. #endif
  6.  
  7. #include <sys/types.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <pwd.h>
  11. #include <stdlib.h>
  12. #include <limits.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15.  
  16. #include <h/envvar_str.h>
  17. #include <h/os.h>
  18. #include <lib/ori_add_lib.h>
  19. /*}}}  */
  20.  
  21. /*{{{  expand leading ~ in pathnames*/
  22. char *home_expand(char * const n)
  23. {
  24.   if (n[0]==HOME_CHAR)
  25.    { char n_buff[_POSIX_PATH_MAX+1];
  26.      char *n_ptr;
  27.      int l;
  28.  
  29.      strcpy(n_ptr=n_buff,n);
  30.      l=strlen(n)-1;
  31.      switch (n[1])
  32.       { case PATH_C:
  33.         case '\0':
  34.          /*{{{  replace by value of $HOME*/
  35.          { static char *h=0;
  36.  
  37.            if (!h)
  38.               h=getenv(HOME);
  39.            if (h && l+strlen(h)<_POSIX_PATH_MAX)
  40.             { n_ptr++;
  41.               strcpy(n,h);
  42.             }
  43.            else
  44.               n[0]='\0';
  45.            break;
  46.          }
  47.          /*}}}  */
  48.         default:
  49.          /*{{{  try to get a passwd entry for it*/
  50.          { char *s;
  51.            struct passwd *x;
  52.  
  53.            s=n;
  54.            while (*s && *s!=PATH_C)
  55.             { s++;
  56.               n_ptr++;
  57.               l--;
  58.             }
  59.            *s='\0';
  60.            x=getpwnam((char *)n+1);
  61. #          ifndef NO_ENDPWENT
  62.               endpwent();
  63. #          endif
  64.            if (x && l+strlen(x->pw_dir)<_POSIX_PATH_MAX)
  65.               strcpy(n,x->pw_dir);
  66.            else
  67.             { n_ptr=n_buff;
  68.               n[0]='\0';
  69.             }
  70.          }
  71.          /*}}}  */
  72.       }
  73.      strcat(n,n_ptr);
  74.    }
  75.  
  76.   return(n);
  77. }
  78. /*}}}  */
  79. /*{{{  getpwent*/
  80. /*{{{  variables*/
  81. #ifdef NO_GETPWENT
  82.    static uid_t uid_val;
  83. #endif
  84. /*}}}  */
  85. /*{{{  start_getpwent*/
  86. void start_getpwent(void)
  87. {
  88. #ifdef NO_GETPWENT
  89. # ifndef UID_START_GETPWENT
  90. #    define UID_START_GETPWENT 0
  91. # endif
  92. # ifndef UID_END_GETPWENT
  93. #    define UID_END_GETPWENT 31
  94. # endif
  95.   uid_val=UID_START_GETPWENT;
  96. # endif
  97. }
  98. /*}}}  */
  99. /*{{{  next_getpwent*/
  100. char const *next_getpwent(void)
  101. { struct passwd *x;
  102.  
  103. #ifdef NO_GETPWENT
  104.    x=(uid_val>UID_END_GETPWENT)?0:getpwuid(uid_val++);
  105. #else
  106.    x=getpwent();
  107. #endif
  108.  
  109.    return(x?x->pw_name:0);
  110. }
  111. /*}}}  */
  112. /*{{{  end_getpwent*/
  113. void end_getpwent(void)
  114. {
  115. # ifndef NO_ENDPWENT
  116.      endpwent();
  117. # endif
  118. }
  119. /*}}}  */
  120. /*}}}  */
  121.